/*	kd:
	
	You heard me. This is where the shop menu opens and closes.
	
	*/

class Gc_Shop_Entry : EventHandler {
	protected Gc_Shop				shop_control;
	protected Gcs_BaseShop			base_shop;
	protected Gc_ShopkeeperList		shopkeepers;
	protected Gc_Rng				rng;
	
	override void NewGame () {
		base_shop	= new("Gcs_BaseShop");
		
		if(base_shop) {
			base_shop.PostBeginPlay();
		}
	}
	
	override void WorldLoaded (WorldEvent event) {
		
		// kd: Setup the shop controls!
		shop_control	= new("Gc_Shop");
		rng				= new("Gc_Rng");
		
		if(shop_control && rng) {
			// console.printf(
				// "\c[fire]M_EvilField: Evil spirit now haunts your shop. "
				// "Please wait warmly.");
			rng.OnBuild();
			shop_control.OnBuild(players [consoleplayer], rng);
		}
		
		// kd: And some additional data that some other handlers need.
		base_shop	= Gcs_BaseShop.Find();
		shopkeepers	= Gc_ShopkeeperLoad.Ptr();
		
		if(base_shop && shop_control) {
			// kd: Propagate this data to our friendos.
			let to_world = Gc_Shop_ToWorld(Gc_Shop_ToWorld.Find("Gc_Shop_ToWorld"));
			
			if(to_world) {
				to_world.SetShop(base_shop);
				to_world.SetShopkeepers(shopkeepers);
				to_world.SetShopControl(shop_control);
			}
		}
	}
	
	// kd: Update the shop control.
	
	override void WorldTick () {
		rng.Next();
		
		if(shop_control.IsActive()) {
			shop_control.NextState();
		}
	}
	
	override bool InputProcess (InputEvent event) {
		if(shop_control.IsActive()) {
			//                                uhuhuhuhu
			shop_control.HandleMouseMove(event.mousex, event.mousey);
		}
		
		return false;
	}
	
	override void RenderOverlay (RenderEvent event) {
		if(shop_control.IsActive()) {
			shop_control.DrawEverything(event);
		}
	}
	
	// kd: Allow opening the shop. You kinda just toggle it. Feels a bit
	// dumb like this but I dunno why.
	
	override void NetworkProcess (ConsoleEvent event) {
		if(event.name ~== "gimbostratus") {
			
			// kd: Pure peers, whereabouts of the server.
			ToggleShop(event.player);
		}
		
		else if(!event.ismanual && event.name ~== "CloseShop") {
			CloseShop(event.player);
		}
	}
	
	protected void OpenShop (int player) {
		let mo = players [player].mo;
		
		// kd: Usually, the handle to the base shop gets lost, so try to get
		// it back.
		if(!base_shop) {
			base_shop = base_shop.Find();
			
			if(!base_shop) {
				return;
			}
		}
		
		// kd: Same for shopkeepers, but we allow starting up the shop even
		// without them.
		if(!shopkeepers) {
			shopkeepers = Gc_ShopkeeperLoad.Ptr();
		}
		
		if(mo && 0 < mo.health && shop_control && mo.CountInv("MenuWheelActive") < 1) {
		
			// kd: This makes it clientsided! A pretty big advantage over good
			// old acs, the shop doesn't run for everybody if just one guy is
			// using it.
			if(player == consoleplayer) {
				let is_open = shop_control.OnOpen(
					base_shop.ActiveShopList(),
					shopkeepers);
				
				if(is_open) {
					SendNetworkEvent("ShopFreeze");
				}
				
				else {
					console.printf(StringTable.Localize("$cant_open_shop"));
					Gcs_ShopSounds.Failure();
				}
			}
		}
	}
	
	protected void CloseShop (int player) {
		let mo = players [player].mo;
		
		if(mo && shop_control && 0 < mo.CountInv("MenuWheelActive")) {
			mo.TakeInventory("MenuWheelActive", 1);
			mo.TakeInventory("NoTiltZone", 1);
			
			if(player == consoleplayer) {
				shop_control.OnClose();
			}
		}
	}
	
	protected void ToggleShop (int player) {
		if(!shop_control) {
			return;
		}
		
		if(0 < players [player].mo.CountInv("MenuWheelActive")) {
			CloseShop(player);
		}
		
		else {
			OpenShop(player);
		}
	}
}
